import { Constant } from "../constant/Constant";

// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;

@ccclass
export default class DogerFixed extends cc.Component {

    @property(cc.Node)
    rightDogoer: cc.Node = null;

    @property(cc.Node)
    leftDogoer: cc.Node = null;

    @property(cc.Node)
    addScore: cc.Node = null;

    @property(cc.Node)
    leftObj: cc.Node = null;
    @property(cc.Node)
    rightObj: cc.Node = null;

    @property
    kind: string = "twoSide";

    start() {
        this.leftDogoer = cc.find("leftdog", this.node);
        this.rightDogoer = cc.find("rightdog", this.node);
        this.node.y = Constant.SCREEN.height / 2;
        switch (this.kind) {
            case "twoSide":
                this.autoFixed(this.rightDogoer, 1);
                this.autoFixed(this.leftDogoer, 2);
                break;
            case "left":
                this.autoFixed_left(this.rightDogoer, 1);
                this.autoFixed_left(this.leftDogoer, 2);
                break;
            case "right":
                this.autoFixed_right(this.rightDogoer, 1);
                this.autoFixed_right(this.leftDogoer, 2);
                break;
        }
        this.addScore.getComponent(cc.BoxCollider).size.width = Constant.SCREEN.width;
    }

    // onLoad () {}
    private autoFixed(dogoer: cc.Node, direction: number) {
        let totalW = this.leftObj.width + this.rightObj.width;
        let screenW = Constant.SCREEN.width;
        let length = dogoer.width = (screenW - totalW) / 2;
        dogoer.getComponent(cc.BoxCollider).size.width = length;
        dogoer.height = dogoer.getComponent(cc.BoxCollider).size.height;
        switch (direction) {
            case 1://右边
                dogoer.x = (screenW - length) / 2;
                break;
            case 2://左边
                dogoer.x = -(screenW - length) / 2;
                break;
            default:
                break;
        }
    }

    private autoFixed_right(dogoer: cc.Node, direction: number) {
        let totalW = this.leftObj.width + this.rightObj.width;
        let screenW = Constant.SCREEN.width;
        let length = dogoer.width = (screenW - totalW) / 2;
        dogoer.getComponent(cc.BoxCollider).size.width = length;
        dogoer.height = dogoer.getComponent(cc.BoxCollider).size.height;
        switch (direction) {
            case 1://右边
                dogoer.x = -(screenW - length) / 2;
                break;
            case 2://左边
                dogoer.x = -(screenW - 3 * length) / 2;
                break;
            default:
                break;
        }
    }

    private autoFixed_left(dogoer: cc.Node, direction: number) {
        let totalW = this.leftObj.width + this.rightObj.width;
        let screenW = Constant.SCREEN.width;
        let length = dogoer.width = (screenW - totalW) / 2;
        dogoer.getComponent(cc.BoxCollider).size.width = length;
        dogoer.height = dogoer.getComponent(cc.BoxCollider).size.height;
        switch (direction) {
            case 1://右边
                dogoer.x = (screenW - length) / 2;
                break;
            case 2://左边
                dogoer.x = (screenW - 3 * length) / 2;
                break;
            default:
                break;
        }
    }
    onLoad() {
    }



    update(dt) {
        this.movingPlaneBySpeed(dt);
    }

    private movingPlaneBySpeed(dt) {
        let t = this;
        let movingNode: cc.Node = t.node;
        if (movingNode.y < -Constant.SCREEN.height) {
            movingNode.active = false;
            movingNode.y = Constant.SCREEN.height / 2;
        } else {
            let action = cc.moveBy(dt, new cc.Vec2(0, -Constant.GAME_SPEED / 2))
            movingNode.runAction(action);
        }
    }
}
